package com.job.createType.simpleFactory.extend;

import com.job.common.exception.ServiceException;
import com.job.createType.simpleFactory.Coffee;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

/**
 * @Author 王博
 * @DateTime 2025-04-09 08:47
 */
public class NewCoffeeFactory {
    private static Map<String, Coffee> coffeeMap = new HashMap<>();

    static {
        try {
            Properties prop = new Properties();
            InputStream is = NewCoffeeFactory.class.getClassLoader().getResourceAsStream("bean.properties");
            prop.load(is);
            Set<Object> keySet = prop.keySet();
            for (Object keyObj : keySet) {
                String key = (String) keyObj;
                String className = prop.getProperty(key);
                Class<?> clazz = Class.forName(className);
                Coffee coffee = (Coffee) clazz.newInstance();
                coffeeMap.put(key,coffee);
            }
        } catch (Exception e) {
            throw new ServiceException("获取文件信息失败", e);
        }
    }

    public static Coffee createCoffee(String type) {
         return coffeeMap.get(type);
    }
}
